#include<iostream>
using namespace std;
#include<unistd.h>
#include<string.h>
#include<sys/wait.h>
#include<sys/types.h>
#include<fcntl.h>

int main(int argc,char* argv[]){
    extern char** environ;
    printf("process is running...\n");
    pid_t id = fork();
    if(id == 0){
        char *const argv_[]={
            "ls",
            "-a",
            "-l",
            "--color=auto",
            nullptr
        };
        char *const myarr[] = {(char*)"test",nullptr};
        
        sleep(1);
        execlp(argv[0],argv[1],nullptr);



        //自定义环境变量
        // char* const myenv_[] =  {(char*)"MYENV=1112222",nullptr};
        // putenv((char*)"MYENV=88888");//手动导入自定义的环境变量
        // execvpe("test",myarr,environ);
        //execle("./test","test",nullptr,environ);

        
        //execle("./test","test",nullptr,myenv_);

        //execl("./test","test",nullptr);//替换了另一个我们自己写的进程
        //execvp("ls",argv_);

        //execlp("ls","ls","-a","-l","--color=auto",nullptr);
        
        //execl("/usr/bin/ls","ls","-a","-l","--color=auto",nullptr);
        exit(1);//这里的代码没有执行
    }

    int status;
    pid_t ret = waitpid(id,&status,0);
    if(ret == id){
        printf("wait success:exit code:%d,sig number:%d\n",(status>>8)&0xff,(status&0x7f));

    }



    // printf("process is running...\n");
    // execl("/usr/bin/ls","ls",nullptr);



    // //这里的打印没有执行，因为它在execl后面，执行完execl后，代码已经被替换了
    // //所以不会再执行后面的代码，所以才要用子进程去进行程序替换。
    // printf("process is done...\n");

    return 0;
}